#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/platform_device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>


 
/* 寄存器物理地址 */
#define CCM_CCGR1_BASE				(0X020C406C)	
#define SW_MUX_GPIO1_IO03_BASE		(0X020E0068)
#define SW_PAD_GPIO1_IO03_BASE		(0X020E02F4)
#define GPIO1_DR_BASE				(0X0209C000)
#define GPIO1_GDIR_BASE				(0X0209C004)
#define REGISTER_LENGTH				4
/**
  * @brief  释放flatform设备模块的时候此函数会执行
  * @note   
  * @param  dev 要释放的设备 
  * @retval 
  */

static void	led_release(struct device *dev)
{
	printk("led device released!\r\n");	
}
/* 设备资源信息，也就是LED0所使用的所有寄存器 */
static struct resource led_resources[] = {
	[0] = {
		.start 	= CCM_CCGR1_BASE,
		.end 	= (CCM_CCGR1_BASE + REGISTER_LENGTH - 1),
		.flags 	= IORESOURCE_MEM,
	},	
	[1] = {
		.start	= SW_MUX_GPIO1_IO03_BASE,
		.end	= (SW_MUX_GPIO1_IO03_BASE + REGISTER_LENGTH - 1),
		.flags	= IORESOURCE_MEM,
	},
	[2] = {
		.start	= SW_PAD_GPIO1_IO03_BASE,
		.end	= (SW_PAD_GPIO1_IO03_BASE + REGISTER_LENGTH - 1),
		.flags	= IORESOURCE_MEM,
	},
	[3] = {
		.start	= GPIO1_DR_BASE,
		.end	= (GPIO1_DR_BASE + REGISTER_LENGTH - 1),
		.flags	= IORESOURCE_MEM,
	},
	[4] = {
		.start	= GPIO1_GDIR_BASE,
		.end	= (GPIO1_GDIR_BASE + REGISTER_LENGTH - 1),
		.flags	= IORESOURCE_MEM,
	},
};

/** 
 *  定义ID匹配数组 
 *  结构体变量名赋值给 struct platform_device 结构体的 .id_entry 成员
 */
struct platform_device_id leddevice_id = {
	.name = "imx6ul-led-device", /* 这里要与 struct platform_device_id 结构体(driver程序)中id数组中某成员相同
									且要与下边的 leddevice 字段相同，可以与驱动中的platform_driver中的name字段不同*/
};

/* platform设备结构体 */
static struct platform_device leddevice = {
	.name = "imx6ul-led-device",
	.id = -1,
	.dev = {
		.release = &led_release,
	},
	.num_resources = ARRAY_SIZE(led_resources),
	.resource = led_resources,
	.id_entry = &leddevice_id,
};

/**
  * @brief  设备模块加载 
  * @note   
  * @param  
  * @retval 
  */
static int __init leddevice_init(void)
{
	printk("led device module load!\r\n");	
	return platform_device_register(&leddevice);
}


/**
  * @brief  设备模块注销
  * @note   
  * @param  
  * @retval 
  */
static void __exit leddevice_exit(void)
{
	printk("led device module unload!\r\n");
	platform_device_unregister(&leddevice);
}

module_init(leddevice_init);
module_exit(leddevice_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ssq");